package com.chaoxuan.uebt.activity;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.model.Address;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.CacheUtils;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.ToastUtils;
import com.chaoxuan.uebt.view.AddressBean;
import com.chaoxuan.uebt.view.AreaPickerView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.w3c.dom.Text;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

public class AddressActivity extends BaseActivity implements View.OnClickListener{

    boolean isnull=true;
    EditText editContactName,editContactMobile,editContactAddress;
    TextView tvContactAddress;
    String province,city;

    private AreaPickerView areaPickerView;
    private List<AddressBean> addressBeans;
    private int[] i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_address);
        initTitle();
        tvContactAddress=(TextView)findViewById(R.id.tv_contact_address);

        editContactName=(EditText) findViewById(R.id.edit_contact_name);
        editContactMobile=(EditText) findViewById(R.id.edit_contact_mobile);
        editContactAddress=(EditText) findViewById(R.id.edit_contact_address);

//        editContactMobile.setFocusable(true);
//        editContactMobile.setFocusableInTouchMode(true);
//        editContactMobile.requestFocus();
//        editContactMobile.requestFocusFromTouch();

        findViewById(R.id.btn_confirm).setOnClickListener(this);
        findViewById(R.id.tv_contact_address).setOnClickListener(this);
        getUserAddress();

        Gson gson = new Gson();
        addressBeans = gson.fromJson(getCityJson(), new TypeToken<List<AddressBean>>() {}.getType());

        areaPickerView = new AreaPickerView(this, R.style.Dialog, addressBeans);
        areaPickerView.setAreaPickerViewCallback(new AreaPickerView.AreaPickerViewCallback() {
            @Override
            public void callback(int... value) {
                province=addressBeans.get(value[0]).getLabel();
                city=addressBeans.get(value[0]).getChildren().get(value[1]).getLabel();
                i=value;
                if (value.length == 3) {
                    tvContactAddress.setText(province + "-" + city + "-" + addressBeans.get(value[0]).getChildren().get(value[1]).getChildren().get(value[2]).getLabel());
                    city=city+addressBeans.get(value[0]).getChildren().get(value[1]).getChildren().get(value[2]).getLabel();
                }else {
                    tvContactAddress.setText(province + "-" + city);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_contact_address:
                areaPickerView.setSelect(i);
                areaPickerView.show();
                break;
            case R.id.btn_confirm:
                addAddress();
                break;
        }
    }

    private String getCityJson() {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = this.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open("region.json")));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    private void getUserAddress(){
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        subscribe(RetrofitUtils.getAPIService().getUserAddress(user.getToken(), Const.DEVICE_TYPE), new BaseSubscriber<Address>() {
            @Override
            public void onSuccess(Address data) {
                if(data==null){
                    isnull=true;
                }else{
                    isnull=false;
                    editContactName.setText(data.getContact_name());
                    editContactMobile.setText(data.getContact_mobile());
                    province=data.getProvince();
                    city=data.getCity();
                    tvContactAddress.setText(data.getProvince()+"-"+data.getCity());
                    editContactAddress.setText(data.getAddress());
                }
            }

        });
    }

    private void addAddress(){
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        String contact_name=editContactName.getText().toString();
        String contact_mobile=editContactMobile.getText().toString();
        String address=editContactAddress.getText().toString();
        if(TextUtils.isEmpty(contact_name)){
            Toast.makeText(this,"请输入收件人",Toast.LENGTH_LONG).show();
            return;
        }else if(TextUtils.isEmpty(contact_mobile)){
            Toast.makeText(this,"请输入手机号",Toast.LENGTH_LONG).show();
            return;
        }else if(TextUtils.isEmpty(province)){
            Toast.makeText(this,"请选择区域",Toast.LENGTH_LONG).show();
            return;
        }else if(TextUtils.isEmpty(address)){
            Toast.makeText(this,"请输入详细地址",Toast.LENGTH_LONG).show();
            return;
        }
        if(isnull)
            subscribe(RetrofitUtils.getAPIService().addAddress(user.getToken(),contact_name,contact_mobile,province,city,address,Const.DEVICE_TYPE), new BaseSubscriber<Address>() {
                @Override
                public void onSuccess(Address data) {
                    ToastUtils.showCustomToast("保存成功");
                    finish();
                }
            });
        else{
            subscribe(RetrofitUtils.getAPIService().editAddress(user.getToken(),contact_name,contact_mobile,province,city,address,Const.DEVICE_TYPE), new BaseSubscriber<Address>() {
                @Override
                public void onSuccess(Address data) {
                    ToastUtils.showCustomToast("保存成功");
                    finish();
                }

            });
        }
    }


}
